package com.yubest.demo.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.yubest.demo.anno.SafetyProcess;
import com.yubest.demo.config.SignValidation;
import com.yubest.demo.dto.DemoReqDTO;
import com.yubest.demo.dto.DemoRespDTO;
import com.yubest.demo.dto.Response;
import com.yubest.demo.dto.ResponseSign;
import com.yubest.demo.util.AESUtil;
import com.yubest.demo.util.DateUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description:接口加密解密统一处理（注解）
 */
@RestController
public class DemoController {



    /**
     * 请求参数解密，响应结果加密
     * c995bdb78ae1569b7981b78a540631c2
     * @param reqDTO
     * @return
     */
    @SafetyProcess(requestDecode  = true,responeEncode  = true)
    @PostMapping(value = "/testEntity")
    public Response<DemoRespDTO> testEntity(@RequestBody DemoReqDTO reqDTO) {
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        return Response.success(respDTO);
    }


    /**
     * 请求参数解密，响应结果加密
     * 61950ba8f43bac4259180884542574c4
     * @param reqDTO
     * @return
     */
    @SafetyProcess(requestDecode = true,responeEncode = true)
    @PostMapping(value = "/testMap")
    public Response<DemoRespDTO> testMap(@RequestBody Map<String,Object> reqDTO) {
        Integer a = MapUtil.getInt(reqDTO,"c");
        String b = MapUtil.getStr(reqDTO,"d");
        System.out.println(a);
        System.out.println(b);
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        return Response.success(respDTO);
    }

    /**
     * 请求验签并且解密
     * 37c12549d92b20d8cae94aa1e2013a3227d434cdb35560d3540ff746c0bbe359583b5fdb352055fbeb5563c792c7d65ef488b3ca4ddb5492e90a4afb8930538c757704f45a4e032206ad9f8a0676156b315ab6079ecd6d12a010fe3ac912d4e5b47a2d0ebf07945f8a7553ed45d09013ba0d266c8197a1c2bab06e8b4fc7a7f0a2401ade5dad0b5e91f3b40f43069ee5
     * @param reqDTO
     * @return
     */
    @PostMapping(value = "/testSign")
    @SafetyProcess(requestDecode = true,responeEncode = true) //数据加解密
    @SignValidation  //验签
    public ResponseSign testSign(@RequestBody Map<String,Object> reqDTO) {
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        ResponseSign r = new ResponseSign();
        r.setSecret("1234");
        r.setCode(200);
        r.setMessage("推送成功");
        r.setData(respDTO);
        return r;
    }

    /**
     * 模拟第三方请求
     * @return
     */
    @RequestMapping(value = "/testRequest")
    public String testRequest() {
        String url = "http://localhost:8080/testSign";
        String p = "37c12549d92b20d8cae94aa1e2013a3227d434cdb35560d3540ff746c0bbe359583b5fdb352055fbeb5563c792c7d65e008460fc6a345d0b72b6defae296930c757704f45a4e032206ad9f8a0676156bf471a933ebbd2b06fa2e154a836943aa5d1b83e96bdc6289395f5ef159933574a6cfb1c14daefa80c22bc2675ab5a2b0a2401ade5dad0b5e91f3b40f43069ee5";
        String body = HttpUtil.createPost(url)
                .contentType("application/json")
                .body(p).execute().body();

        Map<String,Object> dataMap = JSONUtil.parseObj(body);
        String data = MapUtil.getStr(dataMap,"data");

        return data;
    }




    /**
     * 请求参数解密
     * @param reqDTO
     * @return
     */
    @SafetyProcess(requestDecode = true)
    @PostMapping(value = "/test2")
    public Response<DemoRespDTO> test2(@RequestBody DemoReqDTO reqDTO) {
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        return Response.success(respDTO);
    }

    /**
     * 响应结果加密
     * @param reqDTO
     * @return
     */
    @SafetyProcess(responeEncode = true)
    @PostMapping(value = "/test3")
    public Response<DemoRespDTO> test3(@RequestBody DemoReqDTO reqDTO) {
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        return Response.success(respDTO);
    }

    /**
     * 不进行加解密
     * @param reqDTO
     * @return
     */
    @SafetyProcess(requestDecode = false, responeEncode = false)
    @PostMapping(value = "/test4")
    public Response<DemoRespDTO> test4(@RequestBody DemoReqDTO reqDTO) {
        DemoRespDTO respDTO = new DemoRespDTO().setC(3).setD("4");
        return Response.success(respDTO);
    }




}
